global rawdata = "D:\Dropbox\Pollution Fertility\Proof\Replication Code\rawdata"
global workdata = "D:\Dropbox\Pollution Fertility\Proof\Replication Code\workdata"
***=====================CONTENT===========================
***Clean Pollution Data

***Clean Weather Data	

***Prepare Census Data

***Prepare CLDS Data 

***Prepare CFPS Data 

***======================================================

********************************************************************************
*                                                                              *
*                               Pollution Data                                 *
*                                                                              *
********************************************************************************

use "$rawdata\pm25_1998_2017.dta",clear
xtset cityid year
g l_pm25 = l.pm25
g l2_pm25 = l2.pm25
egen mean_pm25 = rowmean(pm25 l_pm25 l2_pm25)
drop city
ren cityid des_city
keep des_city year mean_pm25
save "$workdata\mean_pm25", replace

use "$rawdata\pm25_1998_2017.dta",clear
xtset cityid year
g l_pm25 = l.pm25
g l2_pm25 = l2.pm25
g l3_pm25 = l3.pm25
g l4_pm25 = l4.pm25
g l9_pm25 = l9.pm25
g dif_pm25_2= pm25-l_pm25 
g dif_pm25_3= pm25-l2_pm25 
g dif_pm25_4= pm25-l3_pm25 
g dif_pm25_5= pm25-l4_pm25 
g dif_pm25_10= pm25-l9_pm25 
drop city
ren cityid des_city
keep if year == 2009
keep des_city pm25 dif_*
save "$workdata\pm25_2009", replace

use "$rawdata\pm25_1998_2017.dta",clear
xtset cityid year
g l2_pm25 = l2.pm25
g dif_pm25_3= pm25-l2_pm25 
drop city
ren cityid des_city
keep if year == 2013
keep des_city pm25 dif_*
save "$workdata\pm25_2013", replace

use "$rawdata\pm25_1998_2017.dta",clear
xtset cityid year
g l2_pm25 = l2.pm25
g dif_pm25_3= pm25-l2_pm25 
drop city
ren cityid des_city
keep if year == 2015
keep des_city pm25 dif_*
save "$workdata\pm25_2015", replace

use "$rawdata\pm25_1998_2017.dta",clear
xtset cityid year
g l2_pm25 = l2.pm25
g dif_pm25_3= pm25-l2_pm25 
drop city
ren cityid des_city
keep if year == 2017
keep des_city pm25 dif_*
save "$workdata\pm25_2017", replace

********************************************************************************
*                                                                              *
*                                Weather Data                                  *
*                                                                              *
********************************************************************************
use "$rawdata\city_weather.dta", clear
xtset cityid year
foreach i in spr_avtemp sum_avtemp fal_avtemp win_avtemp avtemp avwind avhumid avpressure tosun torain {
	g l_`i' = l.`i'
	g l2_`i' = l2.`i'
	egen mean_`i' = rowmean(`i' l_`i' l2_`i')
}
ren cityid des_city
keep des_city year mean_*
save "$workdata\mean_weather", replace

use "$rawdata\city_weather.dta", clear
keep cityid year avtemp tosun
xtset cityid year
foreach i in avtemp tosun {
	g l_`i' = l.`i'
	g l2_`i' = l2.`i'
	gen dif_`i'_3 = `i' - l2_`i'
}
ren cityid des_city 
keep if year == 2009
keep des_city year avtemp tosun dif_*
save "$workdata\weather_2009", replace

use "$rawdata\city_weather.dta", clear
keep cityid year avtemp tosun
xtset cityid year
foreach i in avtemp tosun {
	g l_`i' = l.`i'
	g l2_`i' = l2.`i'
	gen dif_`i'_3 = `i' - l2_`i'
}
ren cityid des_city 
keep if year == 2013
keep des_city year avtemp tosun dif_*
save "$workdata\weather_2013", replace

use "$rawdata\city_weather.dta", clear
keep cityid year avtemp tosun
xtset cityid year
foreach i in avtemp tosun {
	g l_`i' = l.`i'
	g l2_`i' = l2.`i'
	gen dif_`i'_3 = `i' - l2_`i'
}
ren cityid des_city 
keep if year == 2015
keep des_city year avtemp tosun dif_*
save "$workdata\weather_2015", replace

use "$rawdata\city_weather.dta", clear
keep cityid year avtemp tosun
xtset cityid year
foreach i in avtemp tosun {
	g l_`i' = l.`i'
	g l2_`i' = l2.`i'
	gen dif_`i'_3 = `i' - l2_`i'
}
ren cityid des_city 
keep if year == 2017
keep des_city year avtemp tosun dif_*
save "$workdata\weather_2017", replace

********************************************************************************
*                                                                              *
*                                  Census Data                                 *
*                                                                              *
********************************************************************************
use "$workdata\census10", clear
sort des_city

*Merging in air pollution
merge m:1 des_city using "$workdata\pm25_2009"
keep if _m == 3
drop _m

*Merging in thermal inversion
sort des_city
merge m:1 des_city using "$workdata\thermal_2009"
tab _m
keep if _m == 3
drop _m

*Merging in city controls
sort des_city
merge des_city using "$workdata\dist"
tab _m
keep if _m == 3
drop _m

sort des_city
merge m:1 des_city using "$workdata\bigcity_dist"
keep if _m == 3
drop _m

*Merging in weather controls
sort des_city
merge des_city using "$workdata\weather_2009"
tab _m
keep if _m == 3
drop _m

*Merging in wind IV
sort des_city 
merge m:1 des_city using "$workdata\total_coal_2009"
keep if _m == 3
drop _m

sort des_city
merge m:1 des_city using "$workdata\housing_2009"
drop if _m == 2
drop _m

merge m:1 des_city using "$workdata\city_geo"
drop if _m == 2
drop _m



*Create city tier
gen cityid=des_city
gen procity=(cityid==1100|cityid==3100|cityid==1200|cityid==5000)
gen subprocity=(cityid==2101|cityid==2102|cityid==2201|cityid==2301| ///
cityid==3201|cityid==3301|cityid==3302|cityid==3502|cityid==3701|cityid==3702|cityid==4201|cityid==4401 ///
|cityid==4403|cityid==5101|cityid==6101 )
gen procapital=(cityid==1301|cityid==1401|cityid==1501|cityid==3401|cityid==3501|cityid==3601| ///
cityid==4101|cityid==4301|cityid==4501|cityid==4601|cityid==5201|cityid==5301|cityid==6201 ///
|cityid==6301|cityid==6401|cityid==6501)

g ranking=2
replace ranking=1 if procity==1
replace ranking=1 if subprocity==1
replace ranking=1 if procapital==1

*Convert into logarithmic form
for var *dist: g ihs_X = asinh(X)
g log_pm25 = log(pm25)

preserve
*Keep the regression sample
drop if single == 1|female==0
keep if (child_num == 0)|(child_num == 1&give_birth==1)|(child_num == 2 &give_birth==1&second_baby == 1)
keep if minority == 0
save "$workdata\census_han_reg", replace
restore 

drop if single == 1|female==0
keep if minority == 1
save "$workdata\census_minority_reg", replace

********************************************************************************
*                                                                              *
*                                   CLDS Data                                  *
*                                                                              *
********************************************************************************
forvalues i = 2013(2)2017 {
	
use "$workdata\CLDS_`i'", clear
sort des_city
merge m:1 des_city using "$workdata\pm25_`i'"
keep if _m == 3
drop _m

sort des_city
merge m:1 des_city using "$workdata\thermal_`i'"
tab _m
drop if _m == 2
drop _m

sort des_city
merge m:1 des_city using "$workdata\city_`i'"
tab _m
drop if _m == 2
drop _m

sort des_city
merge des_city using "$workdata\dist"
tab _m
drop if _m == 2
drop _m

sort des_city
merge des_city using "$workdata\weather_`i'"
tab _m
drop if _m == 2
drop _m

sort des_city
merge m:1 des_city using "$workdata\bigcity_dist"
drop if _m == 2
drop _m

save "$workdata\clds_reg_`i'", replace
}

use "$workdata\clds_reg_2013",clear
append using "$workdata\clds_reg_2015"
append using "$workdata\clds_reg_2017"

keep if male == 0
keep if minority==0

merge m:1 des_city year using "$workdata\other_pollutant"
keep if _m==3
drop _m

save "$workdata\clds_willing",replace


********************************************************************************
*                                                                              *
*                                   CFPS Data                                  *
*                                                                              *
********************************************************************************
*MERGE
use "$workdata\CFPS_2014_clean",clear
merge m:1 des_city year using "$workdata\mean_pm25"
keep if _m == 3
drop _m

merge m:1 des_city year using "$workdata\mean_weather"
keep if _m == 3
drop _m

sort des_city
merge m:1 des_city using "$workdata\dist"
keep if _m == 3
drop _m

sort des_city
merge m:1 des_city using "$workdata\bigcity_dist"
keep if _m == 3
drop _m

merge m:1 des_city year using "$workdata\mean_thermalstrength"
keep if _m == 3
drop _m

save "$workdata\CFPS_2014_exp", replace
